import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;

public class UDPClient {

	/**
	 * Funkcija za dobijanje vremena od servera
	 * Salje joj se 0x1B kao prvi od 48 bajtova, koji oznacava klijentski zahtev
	 * @param address adresa NTP servera
	 * @return niz od 48 bajtova koje NTP server vraca
	 * @throws Exception
	 */
	public byte[] Open(String address) throws Exception {
		DatagramSocket clientSocket = new DatagramSocket();
		InetAddress IPAddress = InetAddress.getByName(address);
		byte[] sendData = new byte[48];
		sendData[0] = 0x1B;
		byte[] receiveData = new byte[48];
		//slanje trenutnog, lokalnog vremena serveru
		NTPUtil.dateToBytes(new Date(), sendData, 40);
		DatagramPacket sendPacket = new DatagramPacket(sendData,
				sendData.length, IPAddress, 123);
		clientSocket.send(sendPacket);
		DatagramPacket receivePacket = new DatagramPacket(receiveData,
				receiveData.length);
		clientSocket.receive(receivePacket);
	//	byte[] retVal = receivePacket.getData();
		clientSocket.close();

		return receiveData;
	}
}
